package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.event.RefreshEvent;
import com.cljz.makepassenger.model.net.bean.InviteListBean;
import com.cljz.makepassenger.present.activity.ChangeTimeP;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;
import com.cljz.makepassenger.widget.XRadioGroup;
import com.cljz.makepassenger.widget.datetimpick.TimeSelector;
import com.jakewharton.rxbinding.view.RxView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/11.
 */

public class ChangeTimeActivity extends BaseActivity<ChangeTimeP> {
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.rb_later)
    RadioButton rbLater;
    @BindView(R.id.rb_noon)
    RadioButton rbNoon;
    @BindView(R.id.rb_tonight)
    RadioButton rbTonight;
    @BindView(R.id.rb_tomorrow_night)
    RadioButton rbTomorrowNight;
    @BindView(R.id.rb_friday)
    RadioButton rbFriday;
    @BindView(R.id.rb_diy)
    RadioButton rbDiy;
    @BindView(R.id.xRadioGroup)
    XRadioGroup xRadioGroup;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_ok)
    ImageView ivOk;
    private String id;
    private String time;

    @Override
    public void initData(Bundle savedInstanceState) {
        InviteListBean.DataBean dataBean = (InviteListBean.DataBean) getIntent().getExtras().getSerializable("data");
        id = dataBean.getInvite_id();
        tvTime.setText(dataBean.getTime());
        tvDate.setText(dataBean.getDate() + " " + dataBean.getWeek());
//        tvTime.setText(GetSystemTimeUtil.getTime());
//        tvDate.setText(GetSystemTimeUtil.getWeekDate());
        if (GetSystemTimeUtil.nightOut()) {
            rbNoon.setEnabled(false);
            rbTonight.setEnabled(false);
        } else if (GetSystemTimeUtil.noonOut()) {
            rbNoon.setEnabled(false);
        }
        if (GetSystemTimeUtil.fridaytout()) {
            rbFriday.setEnabled(false);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_time;
    }

    @Override
    public ChangeTimeP newP() {
        return new ChangeTimeP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        xRadioGroup.setOnCheckedChangeListener(new XRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(XRadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_later:
                        time = GetSystemTimeUtil.getNowTwoLaterDate();
                        break;
                    case R.id.rb_noon:
                        time = GetSystemTimeUtil.getInDetailTime(GetSystemTimeUtil.getPresentSysNoontime());
                        break;
                    case R.id.rb_tonight:
                        time = GetSystemTimeUtil.getInDetailTime(GetSystemTimeUtil.getPresentSysNighttime());
                        break;
                    case R.id.rb_tomorrow_night:
                        time = GetSystemTimeUtil.getTomoreaoyDate();
                        break;
                    case R.id.rb_friday:
                        time = GetSystemTimeUtil.getFridayNightDate();
                        break;
                    case R.id.rb_diy:
                        new TimeSelector(ChangeTimeActivity.this, new TimeSelector.ResultHandler() {
                            @Override
                            public void handle(String time1) {
                                time = time1;
                            }
                        }, GetSystemTimeUtil.getStartTimeandHHMM(), GetSystemTimeUtil.getEndTimeandHHMM()).show();
                        rbDiy.setChecked(false);
                        break;
                }
            }
        });
        RxView.clicks(ivOk)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (time == null) {
                            T.getInstance().showShort("未选择时间");
                        } else {
                            getP().getInviteUpdate(id, time);
                        }
                    }
                });
        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop((Activity) App.getInstance());
                        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                    }
                });
    }

    public void inviteUpdateSucceed() {
        BusProvider.getBus().post(new RefreshEvent());
        T.getInstance().showShort("修改成功");
        finish();
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    public void inviteUpdateFail(String error) {
        T.getInstance().showShort("修改失败，请稍后再试");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
